//
//  JKSleepTimeCell.m
//  JiuKuMusic
//
//  Created by jiukuyinyue on 14-12-1.
//  Copyright (c) 2014年 JiuKuForApp. All rights reserved.
//

#import "JKSleepTimeCell.h"
//#import "PlayViewController.h"

#import "TheHomeManager.h"

@interface JKSleepTimeCell ()
@property (strong, nonatomic)NSArray * timeLabelArray;
@property (assign, nonatomic)BOOL decide;
@property (assign, nonatomic)NSInteger tempNumber;
@end

@implementation JKSleepTimeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.decide = YES;
    
    [self loadSubView];
    self.timeSlider.value = [TheHomeManager shareTheHomeManager].tiemSliderValue;
#pragma mark - 3.23 设置睡眠定时修改
    UIImageView * aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 9, ScreenWidth-60, 8)];
//因数据的原因暂时用上面的代替
//    UIImageView * aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 9, ScreenWidth-60, 8)];
    aImageView.image = [UIImage imageNamed:@"daojishiBar@2x"];
    [self.timeSlider addSubview:aImageView];
    self.timeSlider.maximumTrackTintColor = [UIColor clearColor];
    self.timeSlider.minimumTrackTintColor = [UIColor clearColor];
    self.timeLabelArray = [NSArray arrayWithObjects:self.tenMinutesLable, self.twentyMinutesLabel, self.thirtyMinutesLabel, self.sixtyMinutesLabel, self.ninetyMinutesLabel, nil];
    
    self.tenMinutesLable.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    self.twentyMinutesLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    self.thirtyMinutesLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    self.sixtyMinutesLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    self.ninetyMinutesLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    
    self.tempNumber = -1;
    [self changeSleepTime:self.timeSlider];
}

- (void)loadSubView
{
    
    NSInteger jianju = (ScreenWidth - 240)/4;

//    NSInteger jianju = (ScreenWidth - 240)/4;
    self.tenMinutesLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 40, 15)];
    self.tenMinutesLable.text = @"10分钟";
    self.tenMinutesLable.textColor = [UIColor lightGrayColor];
    self.tenMinutesLable.font = [UIFont systemFontOfSize:8];
    
    self.twentyMinutesLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.tenMinutesLable.right + jianju, 10, 40, 15)];
    self.twentyMinutesLabel.text = @"20分钟";
    self.twentyMinutesLabel.textColor = [UIColor lightGrayColor];
    self.twentyMinutesLabel.font = [UIFont systemFontOfSize:8];
    
    self.thirtyMinutesLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.twentyMinutesLabel.right + jianju, 10, 40, 15)];
    self.thirtyMinutesLabel.text = @"30分钟";
    self.thirtyMinutesLabel.textColor = [UIColor lightGrayColor];
    
    self.thirtyMinutesLabel.font = [UIFont systemFontOfSize:8];
    self.sixtyMinutesLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.thirtyMinutesLabel.right + jianju, 10, 40, 15)];
    self.sixtyMinutesLabel.text = @"60分钟";
    self.sixtyMinutesLabel.textColor = [UIColor lightGrayColor];
    self.sixtyMinutesLabel.font = [UIFont systemFontOfSize:8];
    self.ninetyMinutesLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.sixtyMinutesLabel.right + jianju, 10, 40, 15)];
    self.ninetyMinutesLabel.text = @"90分钟";
    self.ninetyMinutesLabel.textColor = [UIColor lightGrayColor];
    
    self.ninetyMinutesLabel.font = [UIFont systemFontOfSize:8];
    
    self.timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(25, 25, ScreenWidth - 50, 31)];
//    self.timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(25, 25, ScreenWidth - 50, 31)];
    [self.timeSlider addTarget:self action:@selector(changeLabelFram:) forControlEvents:UIControlEventValueChanged];
    [self.timeSlider addTarget:self action:@selector(changeSleepTime:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:_tenMinutesLable];
    [self.contentView addSubview:_twentyMinutesLabel];
    [self.contentView addSubview:_thirtyMinutesLabel];
    [self.contentView addSubview:_sixtyMinutesLabel];
    [self.contentView addSubview:_ninetyMinutesLabel];
    [self addSubview:_timeSlider];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
//touchUP调用
- (void)changeSleepTime:(UISlider *)sender {
    if (sender.value < 0.125) {
        sender.value = 0;
        self.sleepTimeNumber = 60*10;
    }else if (sender.value < 0.375){
        self.sleepTimeNumber = 60*20;
        sender.value = 0.25;
    }else if (sender.value < 0.625){
        self.sleepTimeNumber = 60*30;
        sender.value = 0.5;
    }else if (sender.value < 0.875){
        self.sleepTimeNumber = 60*60;
        sender.value = 0.75;
    }else{
        sender.value = 1.0;
        self.sleepTimeNumber = 60*90;
    }
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(loseTime) userInfo:nil repeats:YES];
    [[TheHomeManager shareTheHomeManager].timer invalidate];
    [TheHomeManager shareTheHomeManager].timer = self.timer;
    [TheHomeManager shareTheHomeManager].tiemSliderValue = sender.value;
}

//睡眠计时
- (void)loseTime
{
    [TheHomeManager shareTheHomeManager].timeLabel.hidden = NO;
    self.sleepTimeNumber = self.sleepTimeNumber - 1;
    if (self.sleepTimeNumber == 0) {
        [self.timer invalidate];
//        PlayViewController * playVC = [TheHomeManager shareTheHomeManager].playVC;
//        if ([TheHomeManager shareTheHomeManager].isPlaying) {
//            [playVC pauseAudio];
//            [playVC sleepTimeChangeIsPlaying];
//        }
//        [playVC.playBtn setBackgroundImage:[UIImage imageNamed:@"new暂停p@3x"] forState:UIControlStateNormal];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sleepEndClick" object:nil];
    }
    NSInteger minutesCount = self.sleepTimeNumber/60;
    NSInteger secondsCount = self.sleepTimeNumber%60;
    if (secondsCount < 10) {
        [TheHomeManager shareTheHomeManager].timeLabel.text = [NSString stringWithFormat:@"%ld:0%ld", (long)minutesCount, (long)secondsCount];
    }else
    [TheHomeManager shareTheHomeManager].timeLabel.text = [NSString stringWithFormat:@"%ld:%ld", (long)minutesCount, (long)secondsCount];
}
//valueChange调用
- (void)changeLabelFram:(UISlider *)sender {
    if (sender.value < 0.12) {
        [self getWillChangeLabel:0];
    }else if (sender.value < 0.37){
        [self getWillChangeLabel:1];
    }else if (sender.value < 0.62){
        [self getWillChangeLabel:2];
    }else if (sender.value < 0.87){
        [self getWillChangeLabel:3];
    }else if (sender.value < 1.0){
        [self getWillChangeLabel:4];
    }
}
- (void)getWillChangeLabel:(NSInteger)number
{
    if (self.tempNumber != number) {
        self.decide = YES;
    }
    if (self.decide == YES) {
        self.decide = NO;
        if (self.tempNumber >= 0) {
            UILabel * oldLabel = [self.timeLabelArray objectAtIndex:self.tempNumber];
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];
            oldLabel.textColor = [UIColor lightGrayColor];
            oldLabel.frame = CGRectMake(oldLabel.frame.origin.x, oldLabel.frame.origin.y+5, oldLabel.frame.size.width, oldLabel.frame.size.height);
            [UIView commitAnimations];
        }
        UILabel * label = [self.timeLabelArray objectAtIndex:number];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        label.textColor = [UIColor blackColor];
        label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y-5, label.frame.size.width, label.frame.size.height);
        [UIView commitAnimations];
        
        self.tempNumber = number;
    }
}
#pragma mark  此处有BUG(该方法未被引用)
- (void)firstStar
{
    CGFloat sliderValue = [TheHomeManager shareTheHomeManager].tiemSliderValue;
    NSInteger number = 0;
    if (sliderValue < 0.12) {
        number = 0;
    }else if (sliderValue < 0.37){
        number = 1;
    }else if (sliderValue < 0.62){
        number = 2;
    }else if (sliderValue < 0.87){
        number = 3;
    }else if (sliderValue < 1.0){
        number = 4;
    }
    UILabel * label = [self.timeLabelArray objectAtIndex:number];
    label.textColor = [UIColor blackColor];
    NSLog(@"%f", label.frame.origin.y);
    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y-5, label.frame.size.width, label.frame.size.height);
    NSLog(@"%f", label.frame.origin.y);
}

@end
